package com.android.antelopeapp.view.Commodity;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.antelopeapp.AntelpopApplication;
import com.android.antelopeapp.R;
import com.android.antelopeapp.activity.BaseActivity;
import com.android.antelopeapp.common.Formater;
import com.android.antelopeapp.constant.ApiConfig;
import com.android.antelopeapp.constant.AppConfig;
import com.android.antelopeapp.constant.MsgConfig;
import com.android.antelopeapp.external.paginglistview.PagingBaseAdapter;
import com.android.antelopeapp.network.GsonRequest;
import com.android.antelopeapp.network.NetworkManager;
import com.android.antelopeapp.view.MyPagingListView;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.lyhw.model.Attach;
import com.lyhw.model.Commodity;
import com.lyhw.model.beanwraper.WraperFactory;

import java.util.List;

/**
 * Created by phk on 15-5-12.
 */
public class CommodityListView extends MyPagingListView<Commodity>{
    private String typeId;

    public CommodityListView(Context context) {
        super(context);
    }

    public CommodityListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CommodityListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void initAdapter() {
        CommodityListAdapter adapter = new CommodityListAdapter();
        this.setAdapter(adapter);
    }

    public void setTypeId(String typeId){
        this.typeId = typeId;
    }

    /**
     * 更新listview
     * */
    protected void updateListView() {
        String url;
        if(typeId==null) {
            url=ApiConfig.REQUEST_GOODS_LIST;
            url = url + "?" + ApiConfig.START_POS + "=" + mStartPos + "&" + ApiConfig.PAGE_SIZE_NAME + "=" + AppConfig.COMMODITY_PAGE_SIZE+"&"+ApiConfig.SORT_BY_CRTIME;
        }else{
            url=ApiConfig.REQUEST_GOODS_LIST_BY_TYPE.replace(ApiConfig.PARAM_TYPEID,typeId);
            url=url + "?" + ApiConfig.START_POS + "=" + mStartPos + "&" + ApiConfig.PAGE_SIZE_NAME + "=" + AppConfig.COMMODITY_PAGE_SIZE+"&"+ApiConfig.SORT_BY_CRTIME;
        }
        GsonRequest<WraperFactory.GoodsListWraper> gsonRequest = new GsonRequest<WraperFactory.GoodsListWraper>(
                Request.Method.GET,
                url,
                WraperFactory.GoodsListWraper.class,
                null,
                new Response.Listener<WraperFactory.GoodsListWraper>() {
                    @Override
                    public void onResponse(WraperFactory.GoodsListWraper response) {
                        List<Commodity> items = response.getData().getContent();
                        if(items!=null) {
                            for (Commodity com : items) {
                                if (com.getAttach() == null) {
                                    com.setAttach(new Attach());
                                }
                            }
                            addPage();
                            addItemsToList(!response.getData().isLast(),items);
                            Log.v("GCF", "load goods list");
                        }else{
                            bacty.sendToastMsg(MsgConfig.GET_ITEMS_FAILURE);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        bacty.sendToastMsg(MsgConfig.GET_ITEMS_FAILURE);
                        Log.v(MsgConfig.GET_ITEMS_FAILURE, error.getMessage());
                    }
                }
        );
        NetworkManager.getInstance(bacty).getRequestQueue().add(gsonRequest);
    }

    class CommodityListAdapter extends PagingBaseAdapter<Commodity> {
        /**
         * position是当前获取view的位置
         * convertView是 Recycler中被替换的view
         * 若convertView为空，则初始化新的ListItem,并将其子控件记录在ViewHolder中，并记录当前的ViewHolder
         * 若convertView不为空，则直接获取ViewHolder，设置对应position的属性即可。
         * */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView == null){
                convertView = LayoutInflater.from(bacty).inflate(R.layout.view_gd_list_item, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            Commodity it = items.get(position);
            viewHolder.setData(it,bacty);
            return convertView;
        }

        class ViewHolder{
            ImageView imgV;
            TextView nameV;
            TextView priceV;
            TextView likeV;
            TextView descV;
            View mainV;
            ViewHolder(View parent){
                mainV = parent;
                imgV = (ImageView) parent.findViewById(R.id.glist_item_img);
                nameV = (TextView) parent.findViewById(R.id.glist_item_name);
                priceV = (TextView) parent.findViewById(R.id.glist_item_price);
                likeV = (TextView) parent.findViewById(R.id.glist_item_like_cnt);
                descV = (TextView) parent.findViewById(R.id.glist_item_desc);
                //commentV = (TextView) parent.findViewById(R.id.glist_item_comment_cnt);
            }
            public void setData(final Commodity it,Context context){
                //todo 为view绑定信息
                //网络加载图片
                ImageLoader imgLoader = NetworkManager.getInstance(context).getImageLoader();
                imgLoader.get(NetworkManager.getGoodsMainPicPath(it.getFirstMainPic()), ImageLoader.getImageListener(imgV,
                        R.drawable.test_equi, R.drawable.test_equi));
                nameV.setText(it.getTitle());
                priceV.setText(Formater.formatPrice(it.getPrice()));
                descV.setText(it.getRecommend());
                //todo like
                /*
                if(it.getAttach()!=null) {
                    likeV.setText(it.getAttach().getPraise());
                }
                */
            }
            /**设置当前view_item显示的信息为等待异步加载*/
            public void setProcessing(){
                //todo 暂时做测试，需要修改
                imgV.setImageResource(R.drawable.test_equi);
                nameV.setText("等待");
                priceV.setText("等等吧");
                likeV.setText("...");
            }
        }
    }
}
